package bounjour;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Vector;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.http.HttpSession;

/**
 * @author Evren Demirkan
 * @date: 22.05.11
 * 
 * This class contains properties and other required methods
 * to register and login a user. Upon a success login/registration a session object
 * is created. This session object is to be checked for private pages.
 */

public class User {

	// Actual name of the user, not user name!
	private String displayName;
	private String userEmail;
	private String userPassword;
	private Date registerDate;
	static private RecordManager rm;
	
	public User(String displayName, String eMail, Date registerDate, String password) {
		this.setDisplayName(displayName);
		this.seteMail(eMail);
		this.setRegisterDate(registerDate);
		this.setPassword(password);
	}

	// Interface functions
	static public boolean login(String eMail, String password) throws SQLException {
		String tableName = "Users";
		Vector <HashMap<String, Object>> resultList;
		ArrayList<String> columnList = new ArrayList<String>();
		columnList.add("*");
		ArrayList<String> columnNameList = new ArrayList<String>();
		columnNameList.add("Email");
		columnNameList.add("Password");
		
		ArrayList<String> columnValueList = new ArrayList<String>();
		columnValueList.add(eMail);
		columnValueList.add(password);
		rm = RecordManager.getRecordManager();
		resultList = rm.selectRecord(columnList, tableName, columnNameList, columnValueList);	
		if (!resultList.isEmpty()){
			// Login seems Ok
			return true;
		}else{
			// Login failed
			return false;
		}
	}

	@SuppressWarnings("rawtypes")
	static public ArrayList<String> register(String displayName, String userEmail, String userPassword, String userPassword2) throws Exception {
		String tableName = "Users";
		ArrayList <String> resultList = new ArrayList<String>();
		rm = RecordManager.getRecordManager();
		// check if user email is already present in the Users table
		// validate e-mail
		if (Util.validateEmail(userEmail)) {

			Vector resultSet = rm.selectRecord("*", tableName, "Email", userEmail);
			if (resultSet.size() > 0) {
				System.out.println("Email is already in use");
				resultList.add("Email is already in use");
			}

		} else {
			resultList.add("Email is in invalid format");
		}
		// Check if passwords match
		if (!userPassword.equals(userPassword2)){
			resultList.add("Passwords do not match");
		}
		
		// all checks passed. insert user into the Users table
		//
		if (resultList.size() == 0) {
			Date registerDate = Util.getCurrentDate();

			ArrayList<String> valueList = new ArrayList<String>();
			valueList.add(displayName);
			valueList.add(userEmail);
			valueList.add(userPassword);
			valueList.add(registerDate.toString());

			ArrayList<String> columnList = new ArrayList<String>();
			columnList.add("DisplayName");
			columnList.add("Email");
			columnList.add("Password");
			columnList.add("RegisterDate");
			int insertionResult = rm.insertRecord(tableName, columnList, valueList);
			if (insertionResult <= 0) {
				System.out.println("Error while creating the user");
				resultList.add("Error while creating the user");
			}

		}
		rm.closeConnection();
		return resultList;
	}

	// TODO
	public User getCurrentUser() {
		return this;
	}

	// TODO
	public boolean isLogin() {
		return getCurrentUser() != null;
	}

	// TODO
	public ArrayList<User> getFollowers() {
		return null;
	}

	// TODO
	public ArrayList<User> getFollowedUsers() {
		return null;
	}

	public String getDisplayName() {
		return this.displayName;
	}

	public String geteMail() {
		return this.userEmail;
	}

	public Date getRegisterDate() {
		return this.registerDate;
	}


	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public void seteMail(String userEmail) {
		if (Util.validateEmail(userEmail)){
		this.userEmail = userEmail;
		}
	}

	public void setRegisterDate(Date registerDate) {
		this.registerDate = registerDate;
	}

	private void setPassword(String userPassword) {
		this.userPassword = userPassword;
	}
}
